﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Stats
{
    /// <summary>
    /// Interaction logic for Grafy.xaml
    /// </summary>
    public partial class Grafy : Window
    {
        enum TypZobrazeni { Roky, Dny, Mesice };
        
        public Grafy()
        {
            if (0 == DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT COUNT(table_name) FROM information_schema.tables WHERE table_name LIKE 'Vracene%'"))   //Kontrola zda v db neco je
            {
                MessageBox.Show("Bohužel nebylo možné načíst statistiky. Zkuste to prosim později.");
                this.Close();
            }
            else
                InitializeComponent();
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
                InitChart();
                AddItemsToCombobox();
                RefreshCBoxObdobi();
        }

        private void InitChart()
        {
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            valueList.Add(new KeyValuePair<string, int>("", 0));
            chartTrzba.DataContext = null;
            chartTrzba.DataContext = valueList;
        }

        private void AddItemsToCombobox()
        {
            cboxZobrazit.Items.Add(new GrafRoky());
            cboxZobrazit.Items.Add(new GrafDny());
            cboxZobrazit.Items.Add(new GrafMesice());
           // cboxZobrazit.Items.Add(TypZobrazeni.Dny);
           // cboxZobrazit.Items.Add(TypZobrazeni.Roky);
            List<KeyValuePair<string, int>> ss = ((IGraf)cboxZobrazit.Items[0]).GetDataKontext(null);
            cboxZobrazit.SelectedIndex = 0;
        }

        private void RefreshCBoxObdobi()
        {
            cboxObdobi.Items.Clear();
            ResfreshGUI();
        }

        private void ResfreshGUI()
        {
            IGraf selectedGraf = (IGraf)cboxZobrazit.SelectedItem;
            //cboxObdobi.Items.Add(selectedGraf.obdobi);
            if (selectedGraf.obdobi != null)
            {
                cboxObdobi.SelectedIndex = 0;
                foreach (string table in selectedGraf.obdobi)
                    cboxObdobi.Items.Add(table.Replace("Vracene", ""));
                if (cboxObdobi.Items.Count > 0)
                    cboxObdobi.SelectedIndex = 0;
            }

            chartTrzba.Title = ((IGraf)cboxZobrazit.SelectedItem).text;
        }

        private void AddRokyToCboxObdobi()
        {
            List<string> tablesVracene = DataLib.Settings.databases.dbPujcky.QueryGetListString("SELECT table_name from information_schema.tables WHERE table_name LIKE 'Vracene%'");
            foreach(string table in tablesVracene)
                cboxObdobi.Items.Add(table.Replace("Vracene",""));
            if (cboxObdobi.Items.Count > 0)
                cboxObdobi.SelectedIndex = 0;
        }

        private void SetVisibilityObsobi(Visibility visibility)
        {
            cboxObdobi.Visibility = visibility;
            lblObdobi.Visibility = visibility;
        }

        private void nacistGraf()
        {
            IGraf selectedGraf = (IGraf)cboxZobrazit.SelectedItem;
            if (cboxObdobi.SelectedIndex == -1)
                chartTrzba.DataContext = selectedGraf.GetDataKontext(null);
            else
                chartTrzba.DataContext = selectedGraf.GetDataKontext(cboxObdobi.SelectedValue.ToString());

            /*   if ((TypZobrazeni)cboxZobrazit.SelectedItem == TypZobrazeni.Roky)
                   ShowData(NacisRoky());
               else if ((TypZobrazeni)cboxZobrazit.SelectedItem == TypZobrazeni.Mesice)
               {
                   //  AddRokyToCboxObdobi();
                   //    NacisMesice();
               }
               else if ((TypZobrazeni)cboxZobrazit.SelectedItem == TypZobrazeni.Dny)
                    ShowData(NacistDny());*/
        }

        private void ShowData(List<Trzba> values)
        {
           // TrzbaCollection trzbyCollection = new TrzbaCollection (values);
           
           List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            if ((TypZobrazeni)cboxZobrazit.SelectedItem == TypZobrazeni.Roky)                               //ZobrazitJakoRoky
                foreach (Trzba value in values)
                    valueList.Add(new KeyValuePair<string, int>(value.Date.ToString("yyyy"), value.Penize));
            else if ((TypZobrazeni)cboxZobrazit.SelectedItem == TypZobrazeni.Dny)                               //ZobrazitJakoRoky
                    foreach (Trzba value in values)
                        valueList.Add(new KeyValuePair<string, int>(value.Date.ToString("dd. MM."), value.Penize));
            
            chartTrzba.DataContext = null;
            chartTrzba.DataContext = valueList;
        }

        private List<Trzba> NacistDny()
        {
            List<DateTime> tablesVracene = DataLib.Settings.databases.dbPujcky.QueryGetDateTimeleList("SELECT min(od) FROM Vracene" + cboxObdobi.SelectedItem + " GROUP BY od");
    
            List<Trzba> trzby = new List<Trzba>();
            foreach (DateTime dateVracene in tablesVracene)
            {
                string date = dateVracene.ToString("yyyy-MM-dd");
                string dateAlfter = dateVracene.AddDays(1).ToString("yyyy-MM-dd");
                int trzba = DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT SUM(cena) FROM Vracene" + cboxObdobi.SelectedItem + " WHERE [od] > '" + date + "' AND [od] < '" + dateAlfter+ "';");
            
                trzby.Add(new Trzba(DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture), trzba));
            }
            return trzby;
        }

        private List<Trzba> NacisRoky()
        {
            List<string> tablesVracene = DataLib.Settings.databases.dbPujcky.QueryGetListString("SELECT table_name from information_schema.tables WHERE table_name LIKE 'Vracene%'");
           List<Trzba> trzby = new List<Trzba>();
            foreach (string tableVracene in tablesVracene)
            {
                 int  trzba  = DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT SUM(cena) FROM "+ tableVracene);
                DateTime date = DateTime.ParseExact(tableVracene.Replace("Vracene",""), "yyyy", CultureInfo.InvariantCulture);
                trzby.Add(new Trzba(date,trzba));
            }
            return trzby;
        }

        private void cboxZobrazit_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           if (this.IsLoaded)
            {
               RefreshCBoxObdobi();
                nacistGraf();
            }
        }
    }
}
